package cn.zuzi.mzitu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CollctPic {
	public static void main(String[] args) {
		DateTime dateTime = new DateTime();
		dateTime.toString("yyyy-MM-dd");
		List<PicBean> picBeanList = getPicList("2018-03-31",args[0]);

		downPic(picBeanList);

	}

	/**
	 * 下载图片
	 * 
	 * @param picBeanList
	 */
	private static void downPic(List<PicBean> picBeanList) {

		for (PicBean picBean : picBeanList) {
			new DownPicThread(picBean).start();
		}

	}

	/**
	 * 获取采集的地址
	 * 
	 * @return
	 */
	private static List<PicBean> getPicList(String dateStr,String picUrl) {

		picUrl=StringUtil.isBlank(picUrl)?PageBean.baseUrl:picUrl;
		List<PicBean> list = new ArrayList<PicBean>();

		try {
			Document document = Jsoup.connect(picUrl).get();
			Element picUl = document.getElementById(PageBean.pageIdString);
			Elements picAndTime = picUl.getElementsByTag("li");
			for (Element element : picAndTime) {
				PicBean picBean = new PicBean();
				Elements spanELl = element.getElementsByTag("span");
				String attr = spanELl.get(0).getElementsByTag("a").get(0).attr("href");
				String title = spanELl.get(0).text();
				String date = spanELl.get(1).text();
				if (!dateStr.equals(date)) {
					//获取首页的图片
					// break;
				}
				picBean.setPicPubDate(date);
				picBean.setTitle(title);
				picBean.setPicBaseUrl(attr);
				list.add(picBean);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return list;
	}

}
